window = globalThis;
base64Encode = function (n) {
    if (window.btoa)
        return window.btoa(n);
    var e = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
    return function (n) {
        var t, r, i, o, a, s, c;
        for (r = i = 0,
                 o = n.length,
                 s = (o -= a = o % 3) / 3 << 2,
             a > 0 && (s += 4),
                 t = new Array(s); r < o;)
            c = n.charCodeAt(r++) << 16 | n.charCodeAt(r++) << 8 | n.charCodeAt(r++),
                t[i++] = e[c >> 18] + e[c >> 12 & 63] + e[c >> 6 & 63] + e[63 & c];
        return 1 == a ? (c = n.charCodeAt(r++),
            t[i++] = e[c >> 2] + e[(3 & c) << 4] + "==") : 2 == a && (c = n.charCodeAt(r++) << 8 | n.charCodeAt(r++),
            t[i++] = e[c >> 10] + e[c >> 4 & 63] + e[(15 & c) << 2] + "="),
            t.join("")
    }(n)
}
encrypt = function (n) {
    var e, t = "e98ae8878c264a7e";

    function r(n) {
        if (/^[\x00-\x7f]*$/.test(n))
            return n;
        for (var e = [], t = n.length, r = 0, i = 0; r < t; ++r,
            ++i) {
            var o = n.charCodeAt(r);
            if (o < 128)
                e[i] = n.charAt(r);
            else if (o < 2048)
                e[i] = String.fromCharCode(192 | o >> 6, 128 | 63 & o);
            else {
                if (!(o < 55296 || o > 57343)) {
                    if (r + 1 < t) {
                        var a = n.charCodeAt(r + 1);
                        if (o < 56320 && 56320 <= a && a <= 57343) {
                            var s = 65536 + ((1023 & o) << 10 | 1023 & a);
                            e[i] = String.fromCharCode(240 | s >> 18 & 63, 128 | s >> 12 & 63, 128 | s >> 6 & 63, 128 | 63 & s),
                                ++r;
                            continue
                        }
                    }
                    throw new Error("Malformed string")
                }
                e[i] = String.fromCharCode(224 | o >> 12, 128 | o >> 6 & 63, 128 | 63 & o)
            }
        }
        return e.join("")
    }

    function i(n) {
        return 4294967295 & n
    }

    function o(n, e, t, r, i, o) {
        return (t >>> 5 ^ e << 2) + (e >>> 3 ^ t << 4) ^ (n ^ e) + (o[3 & r ^ i] ^ t)
    }

    function a(n, e) {
        var t, r = n.length, i = r >> 2;
        0 != (3 & r) && ++i,
            e ? (t = new Array(i + 1))[i] = r : t = new Array(i);
        for (var o = 0; o < r; ++o)
            t[o >> 2] |= n.charCodeAt(o) << ((3 & o) << 3);
        return t
    }

    return null == n || 0 === n.length ? n : (n = r(n),
        t = r(t),
        function (n, e) {
            var t = n.length
                , r = t << 2;
            if (e) {
                var i = n[t - 1];
                if (i < (r -= 4) - 3 || i > r)
                    return null;
                r = i
            }
            for (var o = 0; o < t; o++)
                n[o] = String.fromCharCode(255 & n[o], n[o] >>> 8 & 255, n[o] >>> 16 & 255, n[o] >>> 24 & 255);
            var a = n.join("");
            return e ? a.substring(0, r) : a
        }(function (n, e) {
            var t, r, a, s, c, l, d = n.length, u = d - 1;
            for (r = n[u],
                     a = 0,
                     l = 0 | Math.floor(6 + 52 / d); l > 0; --l) {
                for (s = (a = i(a + 2654435769)) >>> 2 & 3,
                         c = 0; c < u; ++c)
                    t = n[c + 1],
                        r = n[c] = i(n[c] + o(a, t, r, c, s, e));
                t = n[0],
                    r = n[u] = i(n[u] + o(a, t, r, u, s, e))
            }
            return n
        }(a(n, !0), ((e = a(t, !1)).length < 4 && (e.length = 4),
            e)), !1))
}
// cid="2184f9c89fb8b662edb6afe5504b0cae"

// par='appid=201802274651|ctxid=2184f9c89fb8b662edb6afe5504b0cae|a=18320975328|p=Aafbike2985|r=0.8068459182309005';
function get_init(captchaContextId) {
    n = {
        config: {
            "appid": "201802274651",
            "captchaContextId": captchaContextId,
            "account": "18320975328",
            "password": "Aafbike2985",
        }
    }
    var u = "appid=" + n.config.appid + "|ctxid=" + n.config.captchaContextId + "|a=" + n.config.account + "|p=" + n.config.password + "|r=" + Math.random();

    return base64Encode(encrypt(u))
}

// function get_validate(captchaContextId) {
//     // var o='appid=201802274651|ctxid=8173127f0e6138c8026272b4290f9045|type=init|u=|d=30,31,0:31,26,10:34,22,20:37,16,30:38,12,40:40,10,50:42,7,65:45,5,79:49,4,90:52,4,103:56,3,115:61,3,126:65,3,136:69,3,146:73,3,156:76,3,167:80,4,179:82,6,189:85,7,199:86,9,215:88,10,227:88,11,239:89,12,378:88,12,579:87,12,589:86,12,640:86,13,1019|a=18320975328|p=Aafbike2985|t=1020|r=0.9870396828036883';
//     // var o1='appid=201802274651|ctxid=8173127f0e6138c8026272b4290f9045|type=slide|u=8|d=0,0,0:1,0,155:3,0,166:6,0,177:8,0,189:11,0,201:12,0,211:15,-1,221:17,-1,232:19,-1,242:23,-1,252:27,-1,262:31,-1,272:36,-1,282:41,-1,292:46,-1,302:51,-1,312:55,-1,324:60,-1,335:64,-1,345:67,-1,357:70,-1,368:72,-1,379:74,-1,389:75,-1,401:76,-1,412:77,-1,427:78,-1,505:77,-1,951:73,-1,964:68,-1,974:63,-1,984:59,-2,995:53,-3,1006:47,-3,1016:42,-5,1027:35,-5,1038:30,-6,1048:25,-6,1058:22,-6,1068:19,-7,1080:16,-7,1092:15,-7,1103:13,-7,1120:12,-7,1137:11,-7,1224:11,-7,1493:10,-7,1504:9,-7,1534:8,-7,1573:7,-7,3441:7,-7,3451:7,-7,3797:8,-7,3819:9,-7,3841:8,-7,4353:8,-7,4792|a=18320975328|p=Aafbike2985|t=4792|r=0.2493976234702744';
//     var o1=`appid=201802274651|ctxid=${captchaContextId}|type=slide|u=8|d=0,0,0:1,0,155:3,0,166:6,0,177:8,0,189:11,0,201:12,0,211:15,-1,221:17,-1,232:19,-1,242:23,-1,252:27,-1,262:31,-1,272:36,-1,282:41,-1,292:46,-1,302:51,-1,312:55,-1,324:60,-1,335:64,-1,345:67,-1,357:70,-1,368:72,-1,379:74,-1,389:75,-1,401:76,-1,412:77,-1,427:78,-1,505:77,-1,951:73,-1,964:68,-1,974:63,-1,984:59,-2,995:53,-3,1006:47,-3,1016:42,-5,1027:35,-5,1038:30,-6,1048:25,-6,1058:22,-6,1068:19,-7,1080:16,-7,1092:15,-7,1103:13,-7,1120:12,-7,1137:11,-7,1224:11,-7,1493:10,-7,1504:9,-7,1534:8,-7,1573:7,-7,3441:7,-7,3451:7,-7,3797:8,-7,3819:9,-7,3841:8,-7,4353:8,-7,4792|a=18320975328|p=Aafbike2985|t=4792|r=0.2493976234702744`;

//     // n = {
//     //     "appid": "201802274651",
//     //     "ctxid": captchaContextId,
//     //     "type": "init",
//     //     "userresponse": "",
//     //     "data": "54,31,0:58,25,10:62,19,21:65,13,31:68,9,41:69,6,53:70,4,63:71,4,78:72,3,94:73,3,225:74,3,236:74,4,250:76,4,263:77,6,277:78,8,292:78,9,305:79,11,317:79,13,350:80,13,371"
//     // }
//     // var o = "appid=" + n.appid + "|ctxid=" + n.ctxid + "|type=" + n.type + "|u=" + n.userresponse + "|d=" + n.data + "|a=" + 18320975328 + "|p=" + Aafbike2985 + "|t=" + 612 + "|r=" + Math.random();
//     // var o=`appid=201802274651|ctxid=${captchaContextId}|type=init|u=|d=54,31,0:58,25,10:62,19,21:65,13,31:68,9,41:69,6,53:70,4,63:71,4,78:72,3,94:73,3,225:74,3,236:74,4,250:76,4,263:77,6,277:78,8,292:78,9,305:79,11,317:79,13,350:80,13,371|a=18320975328|p=Aafbike2985|t=612|r=0.5366784330599664`;
//     return base64Encode(encrypt(o))
// }

function get_slide(ctxid, type, d, t, r, u) {
    // type='init'时就是无感，type='slide'时传滑块轨迹
    // u: 滑块距离，无感时为空字符串，滑块时传识别出的具体距离值
    u = u || '';  // 如果u是undefined，设为空字符串

    if (r && r!="") {
        rand = r;
    } else {
        rand = Math.random();
    }
    let str = `appid=201802274651|ctxid=${ctxid}|type=${type}|u=${u}|d=${d}|a=18320975328|p=Aafbike2985|t=${t}|r=${rand}`;
    console.log(str)
    return base64Encode(encrypt(str));
}
console.log(get_init("28a1fabca5a6ff0bcac5089dabbad606","init","157,1,0:156,4,10:156,5,21:155,6,107","601","0.6903831209922221",""))
console.log(get_slide("28a1fabca5a6ff0bcac5089dabbad606","init","157,1,0:156,4,10:156,5,21:155,6,107","601","0.6903831209922221",""))
